package com.vca.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vca.common.model.order.StoreOrder;
import com.vca.common.page.CommonPage;
import com.vca.common.request.CardPageRequest;
import com.vca.common.request.RemarkCardsRequest;
import com.vca.common.response.CardPageResponse;
import com.vca.common.response.CardProductResponse;
import com.vca.common.response.StoreOrderInfoResponse;
import com.vca.common.result.CommonResult;
import com.vca.service.service.StoreOrderService;
import com.vca.service.service.UserCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Li
 * @description 礼品卡控制类
 * @date 2023-08-29 13:44
 */

@Slf4j
@RestController
@RequestMapping("api/admin/card")
@Api(tags = "礼品卡控制类")
public class CardController {

    @Autowired
    private UserCardService userCardService;

    @Autowired
    private StoreOrderService orderService;

    @ApiOperation(value = "礼品卡分页")
    @PostMapping("/page")
    @PreAuthorize("hasAuthority('admin:card:page')")
    public CommonResult<CommonPage<CardPageResponse>> page(@RequestBody CardPageRequest request){
        CommonPage<CardPageResponse> commonPage = CommonPage.restPage(userCardService.pageCard(request));
        return CommonResult.success(commonPage);
    }

    @ApiOperation(value = "礼品卡商品分页")
    @PostMapping("/cardPage")
    @PreAuthorize("hasAuthority('admin:card:productPage')")
    public CommonResult<CommonPage<CardProductResponse>> cardProductPage(@RequestBody CardPageRequest request){
        CommonPage<CardProductResponse> commonPage = CommonPage.restPage(userCardService.cardProductPage(request));
        return CommonResult.success(commonPage);
    }

    /**
     * 订单详情
     */
    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/order/info", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('admin:card:order:info')")
    public CommonResult<StoreOrderInfoResponse> info(@RequestParam(value = "orderId") String orderId) {
        StoreOrder order = orderService.getOne(Wrappers.<StoreOrder>lambdaQuery().eq(StoreOrder::getOrderId, orderId));
        StoreOrderInfoResponse info = orderService.info(order.getId());
        info.setPhone(info.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        info.setUserPhone(info.getUserPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        return CommonResult.success(info);
    }

    /**
     * 订单详情
     */
    @ApiOperation(value = "生成礼品卡")
    @RequestMapping(value = "/generate", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id"),
            @ApiImplicitParam(name = "number", value = "生成数量")
    })
    @PreAuthorize("hasAuthority('admin:card:generate')")
    public CommonResult<String> generateCards(@RequestParam(value = "id") Integer id,@RequestParam(value = "number")Integer number){
        return userCardService.generateCards(id,number)?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "批量标记被赠送")
    @PostMapping("/tagged")
    @PreAuthorize("hasAuthority('admin:card:tagged')")
    public CommonResult<String> taggedCards(@RequestBody List<Integer> ids){
        return userCardService.taggedCards(ids)?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "批量备注")
    @PostMapping("/remark")
    @PreAuthorize("hasAuthority('admin:card:remark')")
    public CommonResult<String> remarkCards(@RequestBody RemarkCardsRequest request){
        return userCardService.remarkCards(request)?CommonResult.success():CommonResult.failed();
    }




}
